// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) definits fieldsfirst ansi space safe 
// Source File Name:   SimplePriorityListArbiter.java

package jones.behavior;

import java.util.*;
import jones.*;

// Referenced classes of package jones.behavior:
//            Behavior, PriorityListArbiter, RobotInterface

public class SimplePriorityListArbiter
    implements PriorityListArbiter
{

    private Behavior priorityList[] = null;
    private Map actions = null;
    private SimpleRobotInterface simpleRobot = null;
    private SimpleRobotInterface robotInterface = null;
    private Behavior behavior = null;
    private Behavior chosenBehavior = null;

    public SimplePriorityListArbiter(SimpleRobotInterface robot, Behavior priorityList[])
    {
        this.priorityList = new Behavior[0];
        actions = ((Map) (new HashMap()));
        this.priorityList = priorityList;
        for (int i = 0; i < priorityList.length; i++)
            actions.put(((Object) (priorityList[i])), ((Object) (new ArrayList())));

        simpleRobot = robot;
        robotInterface = new SimpleRobotInterface() {

            public void setLeftWheelSpeed(int leftWheelSpeed)
            {
                final int speed = leftWheelSpeed;
                Runnable action = new Runnable() {

                    public void run()
                    {
                        simpleRobot.setLeftWheelSpeed(speed);
                    }

                }
;
                List list = (List)actions.get(((Object) (behavior)));
                if (list == null)
                {
                    return;
                } else
                {
                    list.add(((Object) (action)));
                    return;
                }
            }

            public void setRightWheelSpeed(int rightWheelSpeed)
            {
                final int speed = rightWheelSpeed;
                Runnable action = new Runnable() {

                    public void run()
                    {
                        simpleRobot.setRightWheelSpeed(speed);
                    }

                }
;
                List list = (List)actions.get(((Object) (behavior)));
                if (list == null)
                {
                    return;
                } else
                {
                    list.add(((Object) (action)));
                    return;
                }
            }

            public void setRotation(double rotation)
            {
                final double rot = rotation;
                Runnable action = new Runnable() {

                    public void run()
                    {
                        simpleRobot.setRotation(rot);
                    }

                }
;
                List list = (List)actions.get(((Object) (behavior)));
                if (list == null)
                {
                    return;
                } else
                {
                    list.add(((Object) (action)));
                    return;
                }
            }

            public void setTranslation(double translation)
            {
                final double trans = translation;
                Runnable action = new Runnable() {

                    public void run()
                    {
                        simpleRobot.setTranslation(trans);
                    }

                }
;
                List list = (List)actions.get(((Object) (behavior)));
                if (list == null)
                {
                    return;
                } else
                {
                    list.add(((Object) (action)));
                    return;
                }
            }

            public int getLeftWheelSpeed()
            {
                return simpleRobot.getLeftWheelSpeed();
            }

            public int getRightWheelSpeed()
            {
                return simpleRobot.getRightWheelSpeed();
            }

            public int getMaxWheelSpeed()
            {
                return simpleRobot.getMaxWheelSpeed();
            }

            public double getWheelBaseLength()
            {
                return simpleRobot.getWheelBaseLength();
            }

            public boolean isBumping()
            {
                return simpleRobot.isBumping();
            }

            public boolean isPushing()
            {
                return simpleRobot.isPushing();
            }

            public int getLeftPhoto()
                throws ValueNotAvailableException
            {
                return simpleRobot.getLeftPhoto();
            }

            public int getRightPhoto()
                throws ValueNotAvailableException
            {
                return simpleRobot.getRightPhoto();
            }

            public boolean isLeftIROn()
            {
                return simpleRobot.isLeftIROn();
            }

            public boolean isRightIROn()
            {
                return simpleRobot.isRightIROn();
            }

            public Behavior getLastChosenBehavior()
            {
                return simpleRobot.getLastChosenBehavior();
            }

            public RemoteControlState getRemoteControlState()
            {
                return simpleRobot.getRemoteControlState();
            }


        }
;
    }

    public RobotInterface getRobotInterface(Behavior behavior)
    {
        this.behavior = behavior;
        return ((RobotInterface) (robotInterface));
    }

    public Behavior[] getPriorityList()
    {
        return priorityList;
    }

    public synchronized void choose()
    {
        boolean isActionChosen = false;
        for (int i = 0; i < priorityList.length; i++)
        {
            List list = (List)actions.get(((Object) (priorityList[i])));
            if (!list.isEmpty())
                if (isActionChosen)
                {
                    list.clear();
                } else
                {
                    chosenBehavior = priorityList[i];
                    for (int j = 0; j < list.size(); j++)
                    {
                        Runnable action = (Runnable)list.get(j);
                        action.run();
                    }

                    list.clear();
                    isActionChosen = true;
                }
        }

    }

    public synchronized Behavior getLastChosenBehavior()
    {
        return chosenBehavior;
    }



}
